<?php
/**
 * Request class definition
 * 
 * @author Sean Crystal <seancrystal@gmail.com>
 * @package MVP-PHP
 */
 
 /**
  * Represents a request to the application to load a page. $mod, $page and $action
  * can be passed as request variables or the front controller will interpret URLs
  * as /$mod/$page/$action/  
  * @package MVP-PHP
  * @subpackage Base
  */ 
class Request extends PropertyObject
{
	/**
     * Constructor
	 * @param string $mod
	 * @param string $page
	 * @param string $action
	 * @param array $values
	 */     	
	public function __construct($mod, $page, $action, $values)
	{
		$this->mod      = $mod;
		$this->page     = $page;
		$this->action   = $action;		
		$this->values   = $values;
		$this->handlers = array();
		
		$this->readOnly[] = 'mod';
		$this->readOnly[] = 'page';
		$this->readOnly[] = 'action';
		$this->readOnly[] = 'values';
	}
	
	/**
     * Add a callback method to be processed when this request is handled
	 * @param string|array $callback
	 */  	
	public function AddHandler($callback)
	{
		array_unshift($this->handlers, $callback);	
	}
	
	/**
     * Execute callback methods in LIFO order. If any callback returns a value of
	 * false, the processing of the callback stack stops.
	 */  	
	public function Handle()
	{
		if (count($this->handlers) > 0) {
			foreach ($this->handlers as $handler) {
				if (is_array($handler)) {
					if (call_user_method($handler[0], $handler[1], $this->values) === false) {
						return;
					}
				} else {
					if (call_user_func($handler, $this->values) === false) {
						return;
					}
				}
			}	
		}
	}
	
	/**
     * first part of the URL requested
	 * @var string
	 */  	
	protected $mod;
	
	/**
     * second part of the URL requested
	 * @var string
	 */  	
	protected $page;
	
	/**
     * third part of the URL requested
	 * @var string
	 */  	
	protected $action;
	
	/**
     * all request values submitted
	 * @var array
	 */  	
	protected $values;
	
	/**
     * callback method stack
	 * @var array
	 */  	
	protected $handlers;
}

?>